

cc.Class({
    extends: cc.Component,

    properties: {

    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this.getComponent()
        if(this.buttonArray[0].active ==true){
            this.showFlag = true
        }else{
            this.showFlag = false
        }
        this.actionDuration = 0.7
    },
    switch(){
        this.showFlag = !this.showFlag
        if(this.showFlag){
            this.arrow.stopAllActions()
            this.arrow.runAction(cc.rotateTo(this.actionDuration ,0).easing(cc.easeCubicActionOut()))
            this.runOut()
        }else{
            this.arrow.stopAllActions()
            this.arrow.runAction(cc.rotateTo(this.actionDuration ,180).easing(cc.easeCubicActionOut()))
            this.runIn()
        }
    },
    runOut(){
        for(let i=0;i<this.buttonArray.length;i++){
            this.buttonArray[i].active = true
            this.buttonArray[i].stopAllActions()
            this.buttonArray[i].runAction(cc.sequence(this.runOutAction(this.buttonArray.length-i),cc.callFunc(
                (button)=>{
                    button.getComponent(cc.Button).interactable = true
                }
             ,this.buttonArray[i])))
        }
    },
    runIn(){
        for(let i=0;i<this.buttonArray.length;i++){
            this.buttonArray[i].stopAllActions()
            this.buttonArray[i].getComponent(cc.Button).interactable = false
            this.buttonArray[i].runAction(cc.sequence(
                this.runInAction(this.buttonArray.length-i),
                cc.callFunc((button)=>{
                    button.active = false
                },this.buttonArray[i])
            ))
        }
    },
    runOutAction(order){
        let action = cc.spawn(
            cc.moveTo(this.actionDuration/this.buttonArray.length*(order) ,cc.v2(0,-order*180)).easing(cc.easeCubicActionOut()),
            cc.rotateTo(this.actionDuration ,0).easing(cc.easeCubicActionOut())
        )
        return action
    },
    runInAction(order){
        let action = cc.spawn(
            cc.delayTime(this.actionDuration- this.actionDuration/this.buttonArray.length*(order)),
            cc.moveTo(this.actionDuration/this.buttonArray.length*(order) ,cc.v2(0,0)).easing(cc.easeCubicActionOut()),
            cc.rotateTo(this.actionDuration ,180).easing(cc.easeCubicActionOut())
        )
        return action
    },
    getComponent(){
        this.arrow = this.node.getChildByName('收纳箭头 上')
        this.buttonArray = this.node.getChildByName('按钮列表').children
    }
    // update (dt) {},
});
